<?php

namespace Frieza\Modules\Live\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Biz\service\course\Course;
use Biz\service\Goods;
use Biz\service\wiki\WikiBasic;
use Doraemon\model\Article as DbArticle;
use Doraemon\model\Blank as DbBlank;
use Doraemon\model\Live\LiveChest as mLiveChest;
use Doraemon\model\Live\LiveChestType as mLiveChestType;
use Doraemon\model\Photos as DbPhotos;
use Doraemon\model\PublishGuide;
use Doraemon\service\Videos\Videos;
use Doraemon\tools\BlockType;
use Doraemon\tools\IdParse;
use Doraemon\tools\ImgUrl;
use Doraemon\tools\Log;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Doraemon\service\Live\LiveRelaContent;
use Doraemon\model\Live\LiveRelaContent as mLiveRelaContent;


class ChestController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content, $id = 0)
    {
        $tab = new Tab();
        $form = new Form(['room_id' => $id]);
        $form->hidden('room_id');
        $form->select('chest_type', '卡片类型')->options(mLiveChestType::CHEST_TYPE_DESC);
        $form->action(url('/admin/live/chest/' . $id . '/saveType'));
        $chestTypeList = mLiveChestType::selectByWhere(['room_id' => $id, 'ORDER' => ['sort' => 'ASC']]);
        $gridModel = new pGridDataModel($chestTypeList);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableCreateButton();
                $grid->disablePagination();
                $grid->disableFilter();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableActions();
                $grid->column('id', 'ID')->sort();
                $grid->column('chest_type', '类型')->display(function () {
                    return mLiveChestType::CHEST_TYPE_DESC[$this->chest_type];
                });
                $grid->column('room_id', '直播ID');
            }
        );
        $html = '<span class="help-block"><i class="fa fa-info-circle"></i>&nbsp;提示：百宝箱类型支持拖拽排序。</span>';
        $tab->add('卡片类型管理', $form->render() . $gridList->render() . $html, true);
        foreach ($chestTypeList as $row) {
            $tab->addLink(mLiveChestType::CHEST_TYPE_DESC[$row['chest_type']], url('/admin/live/chest/' . $id . '/' . $row['chest_type'] . '/list'));
        }
        return $content->header('直播百宝箱')->breadcrumb(
            ['text' => '直播列表', 'url' => url('admin/live/index')],
            ['text' => '直播百宝箱'])
            ->row($tab);
    }

    public function chestType(Request $request, Content $content, $id = 0, $chestType = 0)
    {
        Admin::script($this->setScript());
        $tab = new Tab();
        $form = $this->_getChestForm($id, $chestType);
        $chestTypeList = mLiveChestType::selectByWhere(['room_id' => $id, 'ORDER' => ['sort' => 'ASC']]);
        $list = [];
        if ($id && $chestType) {
            $list = mLiveChest::selectByWhere(['room_id' => $id, 'chest_type' => $chestType, 'chest_state[!]' => mLiveChest::STATUS_DELETE, 'ORDER' => ['sort' => 'ASC']]);
        }
        $noteIds = [];
        $articleIds = [];
        $blankIds = [];
        $guideIds = [];
        $courseIds = [];
        $goodsIds = [];
        $wikiBasicIds = [];
        foreach ($list as $row) {
            switch ($chestType) {
                case mLiveChest::CHEST_TYPE_NOTE:
                    $noteIds[] = $row['obj_id'];
                    break;
                case mLiveChest::CHEST_TYPE_ARTICLE:
                    $articleIds[] = $row['obj_id'];
                    break;
                case mLiveChest::CHEST_TYPE_BLANK:
                    if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_BLANK) {
                        $blankIds[] = $row['obj_id'];
                    } else if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_GUIDE) {
                        $guideIds[] = $row['obj_id'];
                    }
                    break;
                case mLiveChest::CHEST_TYPE_COURSE:
                    $courseIds[] = $row['obj_id'];
                    break;
                case mLiveChest::CHEST_TYPE_GOODS:
                    if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_GOODS) {
                        $goodsIds[] = $row['obj_id'];
                    } else if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_WIKI) {
                        $wikiBasicIds[] = $row['obj_id'];
                    }
                    break;
            }
        }
        $noteList = [];
        $articleList = [];
        $blankList = [];
        $guideList = [];
        $courseList = [];
        $goodsList = [];
        $wikiBasicList = [];
        if ($noteIds) {
            $dbData = DbPhotos::getPhotos($noteIds);
            foreach ($dbData as $row) {
                $row['pic_url'] = '';
                if ($row['pic_id']) {
                    $row['pic_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $row["pic_id"], ImgUrl::IMAGE_SIZE_SQ_PHONE);
                } else if ($row['video_id']) {
                    $videoInfo = Videos::GetCanUseVideosById($row['video_id']);
                    $picId = $videoInfo ? $videoInfo['cover_pic_id'] : '';
                    $row['pic_url'] = $picId ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $picId, ImgUrl::IMAGE_SIZE_SQ_PHONE) : '';
                }
                $noteList[$row['id']] = $row;
            }
        }
        if ($articleIds) {
            $dbData = DbArticle::getBatchArticleByIds($articleIds);

            $liveList = mLiveRelaContent::selectByWhere(['obj_id' => $articleIds ,  'rela_status' =>mLiveRelaContent::STATUS_NORMAL]);
            if($liveList && is_array($liveList)){
                $liveList = array_combine(array_column($liveList,'obj_id') , $liveList);
            }
            foreach ($dbData as $row) {
                $row['pic_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $row['cover_pic_id'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
//                $row['is_related'] = ($liveList[$row['aid']]) ? 1 : 0;
                $row['related_info']  = $liveList[$row['aid']];
                $articleList[$row['aid']] = $row;
            }
        }
        if ($blankIds) {
            $dbData = DbBlank::getBatchBlankByIds($blankIds);
            foreach ($dbData as $row) {
                $row['pic_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $row['cover_pic_id'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
                $blankList[$row['bid']] = $row;
            }
        }
        if ($guideIds) {
            $dbData = PublishGuide::getGuideInfoByIds($guideIds);
            foreach ($dbData as $row) {
                $row['pic_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $row['cover_pic_id_2_0'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
                $guideList[$row['id']] = $row;
            }
        }
        if ($courseIds) {
            $courseList = Course::getListByIds($courseIds);
            //spec_img
        }
        if ($goodsIds) {
            $goodsList = Goods::Goods($goodsIds);
        }
        if ($wikiBasicIds) {
            $wikiBasicList = WikiBasic::getWikiBasicInfoByIds($wikiBasicIds);
        }
        $modelList = [];
        foreach ($list as $row) {
            switch ($chestType) {
                case mLiveChest::CHEST_TYPE_NOTE:
                    $row['title'] = $noteList ? $noteList[$row['obj_id']]['title'] : '';
                    $row['cover_img'] = $noteList ? $noteList[$row['obj_id']]['pic_url'] : '';
                    break;
                case mLiveChest::CHEST_TYPE_ARTICLE:
                    $row['title'] = $articleList ? $articleList[$row['obj_id']]['title'] : '';
                    $row['cover_img'] = $articleList ? $articleList[$row['obj_id']]['pic_url'] : '';
                    if($articleList[$row['obj_id']]['related_info']){
                         if($articleList[$row['obj_id']]['related_info']['room_id'] == $id){
                             $row['is_related'] =1;

                         }else{
                             $row['is_related'] = 2;
                             $row['other_room_id'] = $articleList[$row['obj_id']]['related_info']['room_id'];

                         }
                    }
                   // $row['is_related'] = $articleList ? $articleList[$row['obj_id']]['is_related'] : 0;
                    break;
                case mLiveChest::CHEST_TYPE_BLANK:
                    if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_BLANK) {
                        $row['title'] = $blankList ? $blankList[$row['obj_id']]['title'] : '';
                        $row['cover_img'] = $blankList ? $blankList[$row['obj_id']]['pic_url'] : '';
                    } else if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_GUIDE) {
                        $row['title'] = $guideList ? $guideList[$row['obj_id']]['title'] : '';
                        $row['cover_img'] = $guideList ? $guideList[$row['obj_id']]['pic_url'] : '';
                    }
                    break;
                case mLiveChest::CHEST_TYPE_COURSE:
                    $row['title'] = $courseList ? $courseList[$row['obj_id']]['title'] : '';
                    $row['cover_img'] = $courseList ? $courseList[$row['obj_id']]['spec_img'] : '';
                    break;
                case mLiveChest::CHEST_TYPE_GOODS:
                    if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_GOODS) {
                        $row['title'] = $goodsList ? $goodsList[$row['obj_id']]['title'] : '';
                        $row['cover_img'] = $goodsList ? $goodsList[$row['obj_id']]['cover_img'] : '';
                    } else if ($row['child_type'] == mLiveChest::CHEST_TYPE_CHILD_WIKI) {
                        $row['title'] = $wikiBasicList ? $wikiBasicList[$row['obj_id']]['title'] : '';
                        $row['cover_img'] = $wikiBasicList ? $wikiBasicList[$row['obj_id']]['cover_img'] : '';
                    }
                    break;
            }
            $modelList[] = $row;
        }
        $gridModel = new pGridDataModel($modelList);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $chestType) {
                $grid->disableCreateButton();
                $grid->disablePagination();
                $grid->disableFilter();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->column('id', 'ID')->sort();
                $grid->column('room_id', '直播ID');
                $objHead = mLiveChestType::CHEST_TYPE_DESC[$chestType] . ' ID';
                if ($chestType == mLiveChestType::CHEST_TYPE_NOTE) {
                    $objHead = 'note ID';
                } else if ($chestType == mLiveChestType::CHEST_TYPE_ARTICLE) {

                } else if ($chestType == mLiveChestType::CHEST_TYPE_BLANK) {
                    $objHead = 'ID';
                    $grid->column('child_type', '类型')->display(function () {
                        if ($this->child_type == mLiveChest::CHEST_TYPE_CHILD_BLANK) {
                            return '文章';
                        } else if ($this->child_type == mLiveChest::CHEST_TYPE_CHILD_GUIDE) {
                            return '指南';
                        }
                    });
                } else if ($chestType == mLiveChestType::CHEST_TYPE_COURSE) {

                } else if ($chestType == mLiveChestType::CHEST_TYPE_GOODS) {
                    $objHead = "ID";
                    $grid->column('child_type', '类型')->display(function () {
                        if ($this->child_type == mLiveChest::CHEST_TYPE_CHILD_GOODS) {
                            return '商城商品';
                        } else if ($this->child_type == mLiveChest::CHEST_TYPE_CHILD_WIKI) {
                            return 'WIKI';
                        }
                    });
                }
                if ($chestType != mLiveChest::CHEST_TYPE_NOTE) {
                    $grid->column('title', '名称');
                }
                $grid->column('cover_img', '首图')->width(200)->display(function () {
                    $imgPath = $this->cover_img;
                    $image = strlen($imgPath) ? "<img src='" . $imgPath . "' style='width:80px' class='img img-thumbnail' />" : "";
                    return $image;
                });
                $grid->column('obj_id', $objHead);
                $grid->actions(function (Grid\Displayers\Actions $actions) use($chestType){
                    $actions->disableEdit();
                    $actions->disableView();
                    if($chestType == mLiveChestType::CHEST_TYPE_ARTICLE){
                        if($actions->row->is_related == 1){
                            $actions->prepend('&nbsp;<a class="fa del_rela_room" data-id="' . $actions->row->id . '" href="javascript:void(0);">取消关联</a><br/>');
                        }else if($actions->row->is_related == 2){
                            $actions->prepend('&nbsp;<a class="fa del_other_rela_room" data-id="' . $actions->row->id . '" data-room="' . $actions->row->other_room_id . '"  href="javascript:void(0);">取消上次绑定</a><br/>');
                        }else{
                            $actions->prepend('&nbsp;<a class="fa rela_room" data-id="' . $actions->row->id . '" href="javascript:void(0);">关联直播</a><br/>');

                        }

                    }
                });
            }
        );
        $html = '<span class="help-block"><i class="fa fa-info-circle"></i>&nbsp;提示：百宝箱里的' . mLiveChestType::CHEST_TYPE_DESC[$chestType] . '支持拖拽排序。</span>';
        $tab->addLink('卡片类型管理', url('/admin/live/chest/' . $id) . '/');
        foreach ($chestTypeList as $row) {
            if ($row['chest_type'] == $chestType) {
                $tab->add(mLiveChestType::CHEST_TYPE_DESC[$chestType], $form->render() . $gridList->render() . $html, true);
            } else {
                $tab->addLink(mLiveChestType::CHEST_TYPE_DESC[$row['chest_type']], url('/admin/live/chest/' . $id . '/' . $row['chest_type'] . '/list'));
            }
        }
        return $content->header('直播百宝箱')->breadcrumb(
            ['text' => '直播列表', 'url' => url('admin/live/index')],
            ['text' => '直播百宝箱'])
            ->row($tab);
    }

    private function _getChestForm($roomId, $chestType)
    {
        Admin::script($this->setScript());

        $form = new Form(['room_id' => $roomId, 'chest_type' => $chestType]);
        $form->hidden('room_id');
        $form->hidden('chest_type');
        switch ($chestType) {
            case mLiveChestType::CHEST_TYPE_NOTE:
                $form->text('obj_id', 'note ID');
                break;
            case mLiveChestType::CHEST_TYPE_ARTICLE:
                $form->text('obj_id', '整屋 ID');
                break;
            case mLiveChestType::CHEST_TYPE_BLANK:
                $form->text('obj_id', '文章或指南 ID');
                break;
            case mLiveChestType::CHEST_TYPE_COURSE:
                $form->text('obj_id', '课程 ID');
                break;
            case mLiveChestType::CHEST_TYPE_GOODS:
                $form->radio('child_type', '类型')->options([mLiveChest::CHEST_TYPE_CHILD_GOODS => '商城商品', mLiveChest::CHEST_TYPE_CHILD_WIKI => 'wiki'])->default(mLiveChest::CHEST_TYPE_CHILD_GOODS);
                $form->text('obj_id', 'ID')->help('注意区分商城商品和wiki，wiki ID 不能填写wiki 商品id');
                break;
        }
        $form->action(url('/admin/live/chest/' . $roomId . '/' . $chestType . '/saveChest'));
        return $form;
    }

    public function saveType(Request $request, $id = 0)
    {
        try {
            $params = $request->all();
            $roomId = isset($params['room_id']) ? $params['room_id'] : 0;
            $chestType = $params['chest_type'];
            if ($roomId && $chestType) {
                $result = mLiveChestType::addData($roomId, $chestType, ['room_id' => $roomId, 'chest_type' => $chestType]);
                if ($result) {
                    return redirect(url('/admin/live/chest/' . $roomId) . '/')->with(compact('success'));
                } else {
                    $error = new MessageBag([
                        'title' => '操作失败'
                    ]);
                    return back()->withInput()->with(compact('error'));
                }
            }
        } catch (\Exception $e) {
            Log::debug($e->getMessage(), $e->getFile(), $e->getLine());
            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);
            return back()->withInput()->with(compact('error'));
        }
    }

    public function saveChest(Request $request, $id = 0, $chestType = 0)
    {
        try {
            $params = $request->all();
            $objId = trim($params['obj_id']);
            if (empty($objId)) {
                throw new \Exception('请填写id');
            }
            $childType = isset($params['child_type']) ? $params['child_type'] : 0;
            if ($id && $chestType) {
                switch ($chestType) {
                    case mLiveChest::CHEST_TYPE_NOTE:
                    case mLiveChest::CHEST_TYPE_ARTICLE:
                    case mLiveChest::CHEST_TYPE_COURSE:
                        break;
                    case mLiveChest::CHEST_TYPE_BLANK:
                        $objType = IdParse::getType($objId);
                        if ($objType == BlockType::TYPE_BLANK) {
                            $childType = mLiveChest::CHEST_TYPE_CHILD_BLANK;
                        } else if ($objType == BlockType::TYPE_GUIDE) {
                            $childType = mLiveChest::CHEST_TYPE_CHILD_GUIDE;
                        } else {
                            throw new \Exception('只能添加文章或则指南');
                        }
                        break;
                    case mLiveChest::CHEST_TYPE_GOODS:
                        if ($childType == mLiveChest::CHEST_TYPE_CHILD_GOODS) {
                        } else if ($childType == mLiveChest::CHEST_TYPE_CHILD_WIKI) {
                        } else {
                            throw new \Exception('只能添加商城商品或则wiki');
                        }
                        break;
                }
                $where = [
                    'room_id' => $id,
                    'chest_type' => $chestType,
                    'obj_id' => $objId,
                    'child_type' => $childType,
                ];
                $chestObjInfo = mLiveChest::getByWhere($where);
                $data = [
                    'room_id' => $id,
                    'chest_type' => $chestType,
                    'obj_id' => $objId,
                    'child_type' => $childType,
                    'chest_state' => mLiveChest::STATUS_NORMAL
                ];
                if ($chestObjInfo) {
                    $result = mLiveChest::updateData($data, $where);
                } else {
                    $result = mLiveChest::insertData($data);
                }
                if ($result) {
                    return redirect(url('/admin/live/chest/' . $id . '/' . $chestType . '/list'))->with(compact('success'));
                } else {
                    $error = new MessageBag([
                        'title' => '操作失败'
                    ]);
                    return back()->withInput()->with(compact('error'));
                }
            } else {
                throw new \Exception('不存在的id');
            }
        } catch (\Exception $e) {
            Log::debug($e->getMessage(), $e->getFile(), $e->getLine());
            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);
            return back()->withInput()->with(compact('error'));
        }
    }

    public function sortType(Request $request, $id = 0)
    {
        $sortInfo = $request->input('sort');
        foreach ($sortInfo as $typeId => $sort) {
            mLiveChestType::updateData(['sort' => $sort], ['id' => $typeId, 'room_id' => $id]);
        }
    }

    public function sortObj(Request $request, $id = 0, $chestType = 0)
    {
        $sortInfo = $request->input('sort');
        if ($id && $chestType) {
            foreach ($sortInfo as $chestId => $sort) {
                mLiveChest::updateData(['sort' => $sort], ['id' => $chestId, 'room_id' => $id, 'chest_type' => $chestType]);
            }
        }
    }

    public function delChestObj(Request $request, $id = 0, $chestType = 0, $chestId = 0)
    {
        try {
            if ($id && $chestId) {
                mLiveChest::updateData(['chest_state' => mLiveChest::STATUS_DELETE], ['id' => $chestId, 'room_id' => $id, 'chest_type' => $chestType]);
                return response()->json([
                    'status' => true,
                    'message' => "操作成功",
                ]);

            } else {
                throw new \Exception('不存在的id');
            }
        } catch (\Exception $e) {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
        return response()->json($success);
    }


    public function relaArticle(Request $request, $id = 0)
    {
        try {
            if ($id ) {
                $chestInfo =  mLiveChest::getByWhere(['id' => $id]);
                if(empty($chestInfo)){
                    return response()->json([
                        'status' => false,
                        'message' => "内容不存在",
                    ]);
                }
                $rela_info =  mLiveRelaContent::getByWhere(['obj_id' => $chestInfo['obj_id'] , 'room_id' => $chestInfo['room_id']]);
                if($rela_info ){
                    $update_info = [
                        'obj_id' => $chestInfo['obj_id'],
                        'obj_type' => BlockType::TYPE_ARTICLE,
                        'room_id' => $chestInfo['room_id'],
                        'admin_id' => Admin::user()->id,
                        'rela_status' => mLiveRelaContent::STATUS_NORMAL
                    ];
                    mLiveRelaContent::updateData($update_info , ['id' => $rela_info['id']]);
                }else{
                    $insert_data = [
                        'obj_id' => $chestInfo['obj_id'],
                        'obj_type' => BlockType::TYPE_ARTICLE,
                        'room_id' => $chestInfo['room_id'],
                        'admin_id' => Admin::user()->id,
                        'rela_status' => mLiveRelaContent::STATUS_NORMAL
                    ];
                    mLiveRelaContent::insertData($insert_data);
                }
                \Doraemon\cache\Queue::insertQueue('Article/updateSolr', array(
                    'article_id' => $chestInfo['obj_id']
                ));
                mLiveRelaContent::updateData(['rela_status' => mLiveRelaContent::STATUS_DELETE] , ['room_id' => $chestInfo['room_id'] ,'obj_id[!]'=> $chestInfo['obj_id']]);
                mLiveRelaContent::updateData(['rela_status' => mLiveRelaContent::STATUS_DELETE] , ['room_id[!]' => $chestInfo['room_id'] ,'obj_id'=> $chestInfo['obj_id']]);
                return response()->json([
                    'status' => true,
                    'message' => "操作成功",
                ]);

            } else {
                throw new \Exception('不存在的id');
            }
        } catch (\Exception $e) {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
        return response()->json($success);
    }


    public function delOtherRoom(Request $request, $id = 0)
    {
        try {
            if ($id ) {
                $chestInfo =  mLiveChest::getByWhere(['id' => $id]);
                if(empty($chestInfo)){
                    return response()->json([
                        'status' => false,
                        'message' => "内容不存在",
                    ]);
                }
                $update_data = [
                    'admin_id' => Admin::user()->id,
                    'rela_status' => mLiveRelaContent::STATUS_DELETE
                ];
                mLiveRelaContent::updateData($update_data , ['obj_id' => $chestInfo['obj_id'] ,'room_id[!]' => $chestInfo['room_id'] , 'rela_status' => mLiveRelaContent::STATUS_NORMAL]);
                \Doraemon\cache\Queue::insertQueue('Article/updateSolr', array(
                    'article_id' => $chestInfo['obj_id']
                ));
                return response()->json([
                    'status' => true,
                    'message' => "操作成功",
                ]);
            } else {
                throw new \Exception('不存在的id');
            }
        } catch (\Exception $e) {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
        return response()->json($success);
    }


    public function delrelaArticle(Request $request, $id = 0)
    {
        try {
            if ($id ) {
                $chestInfo =  mLiveChest::getByWhere(['id' => $id]);
                if(empty($chestInfo)){
                    return response()->json([
                        'status' => false,
                        'message' => "内容不存在",
                    ]);
                }
                $update_data = [
                    'admin_id' => Admin::user()->id,
                    'rela_status' => mLiveRelaContent::STATUS_DELETE
                ];
                mLiveRelaContent::updateData($update_data , ['obj_id' => $chestInfo['obj_id'] ,'room_id' => $chestInfo['room_id'] , 'rela_status' => mLiveRelaContent::STATUS_NORMAL]);
                \Doraemon\cache\Queue::insertQueue('Article/updateSolr', array(
                    'article_id' => $chestInfo['obj_id']
                ));
                return response()->json([
                    'status' => true,
                    'message' => "操作成功",
                ]);
            } else {
                throw new \Exception('不存在的id');
            }
        } catch (\Exception $e) {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
        return response()->json($success);
    }


    /*设置scirpt
    *
    * */
    private function setScript()
    {
        return <<<SCRIPT
         $('.rela_room').unbind('click').click(function(){
            var id= $(this).data('id');
            layer.confirm('确定要关联直播吗？', {
                btn: ['确定', '取消']
            }, function (index) {
                layer.close(index);
                  let url = '/admin/live/chest/relaArticle/'+ id;
                let params = {};
                $.post(url, params , function (result) {
                    if (result.status == 1) {
                        layer.msg(result.message);
                        setTimeout(function () {
                            parent.window.location.reload();
                        }, 2000);
                    } else {
                        layer.msg(result.message);
                    }
                });
            });
        });
        
         //删除
         $('.del_rela_room').unbind('click').click(function(){
            var id= $(this).data('id');
            layer.confirm('确定要删除么？', {
                btn: ['确定', '取消']
            }, function (index) {
                layer.close(index);
                  let url = '/admin/live/chest/delrelaArticle/'+ id;
                let params = {};
                $.post(url, params , function (result) {
                    if (result.status == 1) {
                        layer.msg(result.message);
                        setTimeout(function () {
                            parent.window.location.reload();
                        }, 2000);
                    } else {
                        layer.msg(result.message);
                    }
                });
            });
        });
        
        //删除
         $('.del_other_rela_room').unbind('click').click(function(){
            var id= $(this).data('id');
            var room= $(this).data('room');
            layer.confirm('确定要取消上次绑定的直播间'+room+'么？', {
                btn: ['确定', '取消']
            }, function (index) {
                layer.close(index);
                  let url = '/admin/live/chest/delOtherRoom/'+ id;
                let params = {};
                $.post(url, params , function (result) {
                    if (result.status == 1) {
                        layer.msg(result.message);
                        setTimeout(function () {
                            parent.window.location.reload();
                        }, 2000);
                    } else {
                        layer.msg(result.message);
                    }
                });
            });
        });
SCRIPT;
    }


}